package com.mbuswidget;

import java.util.ArrayList;

import android.app.Activity;
import android.appwidget.AppWidgetManager;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.IBinder;
import android.preference.PreferenceManager;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.Toast;
import android.widget.ToggleButton;

public class MBusWidgetConfigurationInfo extends Activity {
	ListView routeList;
	
	// Service related stuff...
	private MBusWidgetService mbusBoundService;
	private boolean mIsBound;
	private ServiceConnection mConnection = new ServiceConnection() {
		public void onServiceConnected(ComponentName className, IBinder service) {
			// This is called when the connection with the service has been
			// established, giving us the service object we can use to
			// interact with the service. Because we have bound to a explicit
			// service that we know is running in our own process, we can
			// cast its IBinder to a concrete class and directly access it.
			mbusBoundService = ((MBusWidgetService.LocalBinder) service)
					.getService();
			// mService = new Messenger((MBusWidgetService.LocalBinder)service);

			// Tell the user about this for our demo.
			// Toast.makeText(MBusWidgetConfigurationStopsAddStopByAllStops.this,
			// R.string.local_service_connected, Toast.LENGTH_SHORT)
			// .show();

			UpdateItemList();
		}

		public void onServiceDisconnected(ComponentName className) {
			// This is called when the connection with the service has been
			// unexpectedly disconnected -- that is, its process crashed.
			// Because it is running in our same process, we should never
			// see this happen.
			mbusBoundService = null;

			// This is called when the connection with the service has been
			// unexpectedly disconnected -- that is, its process crashed.

			// Toast.makeText(MBusWidgetConfigurationStopsAddStopByAllStops.this,
			// R.string.local_service_disconnected, Toast.LENGTH_SHORT)
			// .show();
		}
	};

	void doBindService() {
		// Establish a connection with the service. We use an explicit
		// class name because we want a specific service implementation that
		// we know will be running in our own process (and thus won't be
		// supporting component replacement by other applications).
		getApplicationContext().bindService(new Intent(MBusWidgetConfigurationInfo.this,
				MBusWidgetService.class), mConnection, Context.BIND_AUTO_CREATE);
		mIsBound = true;
	}

	void doUnbindService() {
		if (mIsBound) {
			// Detach our existing connection.
			getApplicationContext().unbindService(mConnection);
			mIsBound = false;
		}
	}

	@Override
	public void onDestroy() {
		doUnbindService();
		super.onDestroy();
	}

	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.widgetconfiginfo);

		// // Not necessary...
		// widgetID = getIntent().getIntExtra(
		// AppWidgetManager.EXTRA_APPWIDGET_ID,
		// AppWidgetManager.INVALID_APPWIDGET_ID);
		//
		// if(widgetID == AppWidgetManager.INVALID_APPWIDGET_ID)
		// finish();
		//

		doBindService();

		routeList = (ListView) findViewById(R.id.routeList);

	}

	public void UpdateItemList() {
		MBusInfo mbusInfo = mbusBoundService.getBusInfo();
		ArrayList<String> routes = new ArrayList<String>();
		if (mbusInfo != null) {
			for(int x = 0; x < mbusInfo.getRoutes().size(); x++) {
				BusRoute route = mbusInfo.getRoutes().get(x);
				String temp = route.getName() + "(" + route.getShortName() + ")";
				routes.add(temp);
			}
		} else {
			routes.add(MBusWidgetConstants.ERROR_CONNECTING_TO_SERVICE);
		}

		routeList.setAdapter(new ArrayAdapter<String>(this,
				android.R.layout.simple_list_item_1, routes));

	}


}
